﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.DataAccess;
using QuanLyKho.Common;
using QuanLyKho.GUI.Library;
using QuanLyKho.GUI.Frm;
using System.Xml;

namespace QuanLyKho.GUI
{
    public partial class ucListSupplier : UserControl
    {
        private int _ID = 0;
        private long _ReID = 0;
        clsDataAccessListSupplier clsdtListSupplier = new clsDataAccessListSupplier();
        frmlistSupplier frm = null;
        private string strName = string.Empty;
        private DataSet dsTemp = new DataSet();
        private string NavClicked = "";
        private int totalRows = 25;

        private enum NavButton
        {
            First = 1,
            Next = 2,
            Previous = 3,
            Last = 4,
        }
        public ucListSupplier()
        {
            InitializeComponent();
            strName = string.Empty;
            binDataListSupplier(strName);
        }

        private void binDataListSupplier(string _strName)
        {
            List<clsListSupplier> ListSupplier = new clsDataAccessListSupplier().SelectAllWithName(_strName);
            DataTable dt = new DataTable("Supplier");
            if (ListSupplier.Count == 0)
            {
                grdListSupplier.DataSource = dt;
                return;
            }
            else
            {
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã NCC", typeof(string));
                dt.Columns.Add("Tên NCC", typeof(string));
                dt.Columns.Add("Địa chỉ", typeof(string));
                dt.Columns.Add("Người đại diện", typeof(string));
                dt.Columns.Add("Điện thoại", typeof(string));
                dt.Columns.Add("Fax", typeof(string));
                DataRow dr;
                int stt = 0;
                foreach (clsListSupplier obj in ListSupplier)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = obj.ID;
                    dr[2] = stt;
                    dr[3] = obj.Code;
                    dr[4] = obj.Name;
                    dr[5] = obj.Address;
                    dr[6] = obj.Manager;
                    dr[7] = obj.Tel;
                    dr[8] = obj.Fax;
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, grdListSupplier, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    grdListSupplier.DataSource = bindingSource1;
                }
                if (grdListSupplier.ColumnCount > 0)
                {
                    grdListSupplier.Columns[1].Visible = false;
                    grdListSupplier.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    grdListSupplier.Columns[2].Visible = false;
                    grdListSupplier.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }
        }
        private void ShowFormAddSupplier(long _ID)
        {
            frmlistSupplier obj = null;
            if (_ID < 1)
                obj = new frmlistSupplier();
            else
                obj = new frmlistSupplier(_ID);
            obj.StartPosition = FormStartPosition.CenterParent;
            obj.ShowInTaskbar = false;
            obj.MaximizeBox = false;
            obj.MinimizeBox = false;
            obj.ShowDialog();
            binDataListSupplier(strName);
        }
        private void tsbFirst_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.First.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListSupplier, NavClicked, lblPaging, totalRows);
        }

        private void tsbPrevious_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Previous.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListSupplier, NavClicked, lblPaging, totalRows);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Next.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListSupplier, NavClicked, lblPaging, totalRows);
        }

        private void tsbLast_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Last.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, grdListSupplier, NavClicked, lblPaging, totalRows);
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            ShowFormAddSupplier(0);
        }

        private void butUpdate_Click(object sender, EventArgs e)
        {
            int i = 0;
            long ID;

            foreach (DataGridViewRow row in grdListSupplier.Rows)
            {
                if ((Convert.ToInt32(row.Cells["status"].Value) == 1) && (Convert.ToInt64(grdListSupplier.CurrentRow.Cells["status"].Value) == 1))
                    i++;

            }
            if (i > 1)
            {
                new clsLibrary().runLoading("Chọn một nhà cung cấp muốn sửa!", 0);
                HavitelMessageBox.Show("Chỉ được chọn một nhà cung cấp!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
            if (i == 1)
            {
                ID = Convert.ToInt64(grdListSupplier.CurrentRow.Cells["ID"].Value.ToString());
                ShowFormAddSupplier(ID);
                new clsLibrary().runLoading("Đã sửa thành công!", 2);

            }
            if (i == 0)
            {
                new clsLibrary().runLoading("Chọn nhà cung cấp muốn sửa!", 0);
                HavitelMessageBox.Show("Chọn nhà cung cấp muốn sửa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            clsDataAccessListSupplier listDataAccessListSupplier = new clsDataAccessListSupplier();
            if (Convert.ToInt64(grdListSupplier.CurrentRow.Cells["status"].Value) == 0)
                HavitelMessageBox.Show("Chọn tên nhà cung cấp muốn sửa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            else
            {
                if (HavitelMessageBox.Show("Bạn có muốn sửa thông tin nhà cung cấp này?", "Thông báo", HavitelMessageBox.HavitelMessageButton.OKCancel, HavitelMessageBox.HavitelMessageIcon.Question) == DialogResult.OK)
                {
                    foreach (DataGridViewRow row in grdListSupplier.Rows)
                    {
                        if (Convert.ToInt32(row.Cells["status"].Value) == 1)
                        {
                            long ID = Convert.ToInt64(row.Cells["ID"].Value.ToString());
                            listDataAccessListSupplier.Delete(ID);
                            new clsLibrary().runLoading("Đã xóa nhà cung cấp!", 2);
                        }

                    }
                }
                else
                {
                    binDataListSupplier(strName);
                }
            }
            binDataListSupplier(strName);
        }

        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCheckAll.Checked == true)
            {
                foreach (DataGridViewRow row in grdListSupplier.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = true;
                }
            }
            else
            {
                foreach (DataGridViewRow row in grdListSupplier.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = false;
                }
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void grdListSupplier_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void butAdd_Click_1(object sender, EventArgs e)
        {
            ShowFormAddSupplier(0);
        }

        private void butInXml_Click(object sender, EventArgs e)
        {
            OpenFileDialog Open = new OpenFileDialog();
            DataTable dt = new DataTable();
            if (Open.ShowDialog() == DialogResult.OK)
            {
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml("" + Open.FileName + "");
                DataSet dsXml = new DataSet("List");
                dsXml = xmlDatadoc.DataSet;
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã NCC", typeof(string));
                dt.Columns.Add("Tên NCC", typeof(string));
                dt.Columns.Add("Ghi chú", typeof(string));
                dt.Columns.Add("Địa chỉ", typeof(string));
                dt.Columns.Add("Điện thoại", typeof(string));
                dt.Columns.Add("Fax", typeof(string));
                dt.Columns.Add("Website", typeof(string));
                dt.Columns.Add("Email", typeof(string));
                dt.Columns.Add("Người đại diện", typeof(string));
                dt.Columns.Add("Chức vụ", typeof(string));
                dt.Columns.Add("Điện thoại liên lạc", typeof(string));
                dt.Columns.Add("Số Fax", typeof(string));
                dt.Columns.Add("Mail liên lạc", typeof(string));
                DataRow dr;
                int stt = 0;
                foreach (DataRow Row in dsXml.Tables[0].Rows)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = Row["ID"].ToString();
                    dr[2] = stt;
                    dr[3] = Row["Code"].ToString();
                    dr[4] = Row["Name"].ToString();
                    dr[5] = Row["Des"].ToString();
                    dr[6] = Row["Address"].ToString();
                    dr[7] = Row["Tel"].ToString();
                    dr[8] = Row["Fax"].ToString();
                    dr[9] = Row["Website"].ToString();
                    dr[10] = Row["Email"].ToString();
                    dr[11] = Row["Manager"].ToString();
                    dr[12] = Row["Position"].ToString();
                    dr[13] = Row["ContactTel"].ToString();
                    dr[14] = Row["ContactFax"].ToString();
                    dr[15] = Row["ContactEmail"].ToString();
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, grdListSupplier, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    grdListSupplier.DataSource = bindingSource1;
                }
                if (grdListSupplier.ColumnCount > 0)
                {
                    grdListSupplier.Columns[1].Visible = false;
                    grdListSupplier.Columns[2].Visible = false;
                    grdListSupplier.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//STT
                    grdListSupplier.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//MA NCC
                    grdListSupplier.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//TEN NCC
                    grdListSupplier.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[9].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[10].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    grdListSupplier.Columns[11].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[12].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[13].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[14].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                    grdListSupplier.Columns[15].Visible = false;
                    grdListSupplier.Columns[16].Visible = false;
                    //   grdListSupplier.Columns[17].Visible = false;
                }
            }
        }

        private void butOutXml_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog.Title = "Chọn file XML";
            List<clsListSupplier> ListSupplier = new clsDataAccessListSupplier().ListSupplierSelectAll();
            DataTable dt = new DataTable("listSupplier");
            dt.Columns.Add("ID", typeof(long));
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Des", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            dt.Columns.Add("Tel", typeof(string));
            dt.Columns.Add("Fax", typeof(string));
            dt.Columns.Add("Website", typeof(string));
            dt.Columns.Add("Email", typeof(string));
            dt.Columns.Add("Manager", typeof(string));
            dt.Columns.Add("Position", typeof(string));
            dt.Columns.Add("ContactTel", typeof(string));
            dt.Columns.Add("ContactFax", typeof(string));
            dt.Columns.Add("ContactEmail", typeof(string));
            DataRow dr;
            int stt = 0;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (clsListSupplier obj in ListSupplier)
                {
                    dr = dt.NewRow();
                    dr[0] = obj.ID;
                    dr[1] = obj.Code;
                    dr[2] = obj.Name;
                    dr[3] = obj.Des;
                    dr[4] = obj.Address;
                    dr[5] = obj.Tel;
                    dr[6] = obj.Fax;
                    dr[7] = obj.Website;
                    dr[8] = obj.Email;
                    dr[9] = obj.Manager;
                    dr[10] = obj.Position;
                    dr[11] = obj.ContactTel;
                    dr[12] = obj.ContactFax;
                    dr[13] = obj.ContactEmail;
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet("ListSupplier");
                ds.Tables.Add(dt);
                ds.WriteXml(saveFileDialog.FileName, XmlWriteMode.IgnoreSchema);
                new clsLibrary().runLoading("Đã lưu dữ liệu ra file : '" + saveFileDialog.FileName + "'.", 2);
            }
        }




    }
}
